/// <reference path="../../wx/ad/WxBannerAd.ts" />
namespace mgp.ads {
    export class OppoBannerAd extends WxBannerAd {

        protected _bannerAd: any;

        protected _bannerShow() {
            this._bannerAd.show();
            this._bannerShowing = true;
        }
        protected _bannerReadyShow() {
        }
        protected _getBannerAd(id?: string) {
            if (!this._bannerAd) {
                this._bannerAd = this._createBanner(id);
                this._bannerAddListener()
                if (this._info.aliveTime > 0) {
                    this._timeKey = timerOnce(this._info.aliveTime * 1000, this, this._aliveTimeUp, [], true, this._timeKey)
                }
            }
            return this._bannerAd;
        }
        protected _createBanner(id?: string): any {
            let idx = Math.floor(Math.random() * this._info.ids.length);
            return qg.createBannerAd({
                adUnitId: id || this._info.ids[idx],
                style: this._getDefStyle()
            });
        }
        protected _bannerAddListener() {
            if (!this._bannerAd) return;
            this._bannerAd.onError(this._onBannerErr.bind(this))
            this._bannerAd.onLoad(this._onBannerLoaded.bind(this))
            this._bannerAd.onResize(this._onBannerResize.bind(this))
            this._bannerAd.onShow(this._onBannerShow.bind(this))
            this._bannerAd.onHide(this._onBannerHide.bind(this))
        }

        protected _bannerRemoveListener() {
            if (!this._bannerAd) return;
            this._bannerAd.offResize(this._onBannerResize.bind(this))
            this._bannerAd.offError(this._onBannerErr.bind(this))
            this._bannerAd.offLoad(this._onBannerLoaded.bind(this))
            this._bannerAd.offShow(this._onBannerShow.bind(this))
            this._bannerAd.offHide(this._onBannerHide.bind(this))
        }
        protected _getDefStyle(width?: number, height?: number) {
            return null;
        }
        protected _onBannerResize(res) {
            if (!this._bannerAd) return;
            debugLog('banner resize', res)
            if (!!this._info.resize) {
                let { left, top } = this._info.resize(res);
                this._bannerAd.style.left = left;
                this._bannerAd.style.top = top;
            }
        }
        protected _onBannerShow() {
            debugLog('banner广告显示')
            if (!!this._info.shown) this._info.shown();
        }
        protected _onBannerHide() {
            debugLog('banner广告隐藏')
        }
        
        protected _onBannerLoaded(res) {
            super._onBannerLoaded(res);
            this._bannerShow()
        }
    }
}